package com.chinanet.service.faultanticipate.flowmodel;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by xuzhi on 2016/9/28.
 */
public class Translate_data {


    /**
     * 执行方法
     * @param content
     * @return
     */
    public String init(String content,Map<String,Object> value) {
        StringTokenizer st = new StringTokenizer(content, "且|或", true);
        List<String> a = new ArrayList<String>();
        while (st.hasMoreElements()) {
            a.add(st.nextToken());
            // String token = st.nextToken();

        }
        int i = 0;
        List<String> calcu = new ArrayList<>();
        List<Integer> mark = new ArrayList<>();
        List<Boolean> results = new ArrayList<>();
        for (String b : a) {
            if (i % 2 == 0) {
                calcu.add(b);

            } else {
                if ((b.indexOf("且")) != -1) {
                    mark.add(1);

                } else if ((b.indexOf("或")) != -1) {
                    mark.add(2);

                }
            }
            i++;
        }
        for (String s : calcu) {
            if (compare(s,value) == 1) {
                results.add(true);
            } else if (compare(s,value) == 0) {
                results.add(false);
            } else if (compare(s,value) == -1) {
                return "error:字符串非法字符";
            }
        }
        System.out.println(results);
        Boolean in = allcompare(results, mark);
        if(in){
            return "true";
        }else{
            return "false";
        }


    }

    /**
     * 所有的一起比较
     * @param results
     * @param mark
     * @return
     */
    private  Boolean allcompare(List<Boolean> results, List<Integer> mark) {
        boolean first = false;
        if (mark.size() == 0) {
            return results.get(0);
        } else {

            if (mark.get(0) == 1) {
                first = results.get(0) && results.get(1);
            } else {
                first = results.get(0) || results.get(1);
            }

            for (int r = 1, j = 2; j < results.size(); j++, r++) {
                if (mark.get(r) == 1) {
                    first = first && results.get(j);
                } else {
                    first = first || results.get(j);
                }
            }

        }

        return first;
    }

    /**
     * 每个断的比较
     * @param comp
     * @return
     */
    // 1:true；0：false;-1:error
    private  int compare(String comp,Map<String,Object> value) {
        double leftnum = 0, rightnum = 0;
        if (comp.indexOf(">=") != -1) {
            String a[] = comp.split(">=");
            try {
                leftnum = Double.parseDouble(String.valueOf((Object)value.get(a[0].trim())));
                if(a[1].indexOf("&&")!=-1){
                    a[1]=a[1].replaceAll("&&", "");
                    rightnum = Double.parseDouble(String.valueOf((Object)value.get(a[1].trim())));
                }else{
                    rightnum = Double.parseDouble(a[1]);
                }
            } catch (Exception e) {

                e.printStackTrace();
                return -1;
            }
            if (leftnum >= rightnum) {
                return 1;
            } else {
                return 0;
            }
        }else if (comp.indexOf("<=") != -1) {
            String a[] = comp.split("<=");
            try {
                leftnum = Double.parseDouble(String.valueOf((Object)value.get(a[0].trim())));
                if(a[1].indexOf("&&")!=-1){
                    a[1]=a[1].replaceAll("&&", "");
                    rightnum = Double.parseDouble(String.valueOf((Object)value.get(a[1].trim())));
                }else{
                    rightnum = Double.parseDouble(a[1]);
                }
            } catch (Exception e) {

                e.printStackTrace();
                return -1;
            }
            if (leftnum <= rightnum) {
                return 1;
            } else {
                return 0;
            }
        } else if (comp.indexOf(">") != -1) {
            String a[] = comp.split(">");
            try {
                leftnum = Double.parseDouble(String.valueOf((Object)value.get(a[0].trim())));
                if(a[1].indexOf("&&")!=-1){
                    a[1]=a[1].replaceAll("&&", "");
                    rightnum = Double.parseDouble(String.valueOf((Object)value.get(a[1].trim())));
                }else{
                    rightnum = Double.parseDouble(a[1]);
                }
            } catch (Exception e) {

                e.printStackTrace();
                return -1;
            }
            if (leftnum > rightnum) {
                return 1;
            } else {
                return 0;
            }
        } else if (comp.indexOf("<") != -1) {
            String a[] = comp.split("<");

            try {
                leftnum = Double.parseDouble(String.valueOf((Object)value.get(a[0].trim())));
                if(a[1].indexOf("&&")!=-1){
                    a[1]=a[1].replaceAll("&&", "");
                    rightnum = Double.parseDouble(String.valueOf((Object)value.get(a[1].trim())));
                }else{
                    rightnum = Double.parseDouble(a[1]);
                }
            } catch (Exception e) {

                // e.printStackTrace();
                return -1;
            }
            if (leftnum < rightnum) {
                return 1;
            } else {
                return 0;
            }

        } else if (comp.indexOf("=") != -1) {
            String a[] = comp.split("=");
            String right="";
            if(a[1].indexOf("&&")!=-1){
                a[1]=a[1].replaceAll("&&", "");
                right = String.valueOf((Object)value.get(a[1].trim()));
            }else{
                right=a[1].trim();
            }
            if (String.valueOf((Object)value.get(a[0].trim())) .equals(right)) {
                return 1;
            } else {
                return 0;
            }
        } else if (comp.indexOf("包含") != -1) {
            String a[] = comp.split("包含");
            String right="";
            if(a[1].indexOf("&&")!=-1){
                a[1]=a[1].replaceAll("&&", "");
                right = String.valueOf((Object)value.get(a[1].trim()));
            }else{
                right=a[1].trim();
            }
            if (String.valueOf((Object)value.get(a[0].trim())).indexOf(right) != -1) {
                return 1;
            } else {
                return 0;
            }

        } else if (comp.indexOf("正则") != -1) {
            String a[] = comp.split("正则");
            String right="";
            if(a[1].indexOf("&&")!=-1){
                a[1]=a[1].replaceAll("&&", "");
                right = String.valueOf((Object)value.get(a[1].trim()));
            }else{
                right=a[1].trim();
            }
            return isRegex(String.valueOf((Object)value.get(a[0].trim())),right);
        }

        return -1;
    }

    /**
     * 正则判断
     * @param mobiles
     * @param regex
     * @return
     */
    private  int isRegex(String mobiles,String regex) {
        if(match(regex, mobiles)){

            return 1;
        }else{

            return 0;
        }
    }
    /**
     * 正则解析
     * @param regex
     * @param str
     * @return
     */
    private  boolean match(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

}

